import os
import time
from multiprocessing import Process, Queue

# 生产者
def producer(q):
    print(f'producer PID: {os.getpid()}')
    with open('1.txt', 'r') as f:
        # 逐行读取
        for i in f.readlines():
            # 将i插入队列
            q.put(i)
            time.sleep(1)

# 消费者
def consumer(q):
    print(f'consumer PID: {os.getpid()}')
    while True:
        # 获取队列中的内容
        i = q.get()
        print(f'获得生产者进程产生的内容：{i}')

if __name__ == "__main__":
    q = Queue()
    p = Process(target=producer, args=(q,))
    c = Process(target=consumer, args=(q,))
    # 产生数据
    p.start()
    # 消费数据
    c.start()
    # 主进程挂起等待子进程producer
    p.join()
    # 子进程consumer是死循环，无法等待其运行结束，只可强行终止其运行
    c.terminate()